package kr.ac.skhu.m.controller.etc;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.ac.skhu.m.domain.Comment;
import kr.ac.skhu.m.domain.Used;
import kr.ac.skhu.m.service.UsedS;
import kr.ac.skhu.m.util.PaggingUtil;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class UsedC extends AbstractController {
	
	private String successView;
	private int mode;
	private UsedS usedS;
	
	public void setSuccessView(String successView) {
		this.successView = successView;
	}
	public void setMode(int mode) {
		this.mode = mode;
	}
	public void setUsedS(UsedS usedS) {
		this.usedS = usedS;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = null;
		
		switch (mode) {
			case 1:
				mav = list(request, response);
				break;
			case 2:
				mav = view(request, response);
				break;
			case 3:
				mav = write(request, response);
				break;
			case 4:
				mav = update(request, response);
				break;
			case 5:
				mav = delete(request, response);
				break;
		}
		
		return mav;
	}
	
	private ModelAndView list(HttpServletRequest request,
			HttpServletResponse response){
		
		ModelAndView mav = new ModelAndView(successView);
		
		int pageNum = ServletRequestUtils.getIntParameter(request, "pageNum", 1);
		int pNum = 10*(pageNum-1);
		
		List<Used> list = (List<Used>)usedS.getUsedList(pNum);
		int totalPage = usedS.getTotalPage();
		
		PaggingUtil paggingUtil = new PaggingUtil(pageNum, totalPage);
		
		mav.addObject("pageNum", pageNum);
		mav.addObject("list", list);
		mav.addObject("paggingUtil", paggingUtil);
		
		return mav;
		
	}
	
	private ModelAndView view(HttpServletRequest request,
			HttpServletResponse response){
		
		ModelAndView mav = new ModelAndView(successView);
		
		int pageNum = ServletRequestUtils.getIntParameter(request, "pageNum", 1);
		String articleNo = ServletRequestUtils.getStringParameter(request, "articleNo", "");
		
		Used used = (Used)usedS.getUsed(articleNo);
		List<Comment> commentList = (List<Comment>)usedS.getCommentList(articleNo);
		
		mav.addObject("articleNo", articleNo);
		mav.addObject("commentList", commentList);
		mav.addObject("pageNum", pageNum);
		mav.addObject("used", used);
		 
		return mav;
		
	}
	
	private ModelAndView write(HttpServletRequest request,
			HttpServletResponse response){
		
		ModelAndView mav = new ModelAndView(successView);
		
		int pageNum = ServletRequestUtils.getIntParameter(request, "pageNum", 1);
		int pNum = 10*(pageNum-1);
		
		List<Used> list = (List<Used>)usedS.getUsedList(pNum);
		int totalPage = usedS.getTotalPage();
		
		PaggingUtil paggingUtil = new PaggingUtil(pageNum, totalPage);
		
		mav.addObject("list", list);
		mav.addObject("paggingUtil", paggingUtil);
		
		return mav;
		
	}
	
	private ModelAndView update(HttpServletRequest request,
			HttpServletResponse response){
		
		ModelAndView mav = new ModelAndView(successView);
		
		int pageNum = ServletRequestUtils.getIntParameter(request, "pageNum", 1);
		int pNum = 10*(pageNum-1);
		
		List<Used> list = (List<Used>)usedS.getUsedList(pNum);
		int totalPage = usedS.getTotalPage();
		
		PaggingUtil paggingUtil = new PaggingUtil(pageNum, totalPage);
		
		mav.addObject("list", list);
		mav.addObject("paggingUtil", paggingUtil);
		
		return mav;
		
	}
	
	private ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response){
		
		ModelAndView mav = new ModelAndView(successView);
		
		int pageNum = ServletRequestUtils.getIntParameter(request, "pageNum", 1);
		int pNum = 10*(pageNum-1);
		
		List<Used> list = (List<Used>)usedS.getUsedList(pNum);
		int totalPage = usedS.getTotalPage();
		
		PaggingUtil paggingUtil = new PaggingUtil(pageNum, totalPage);
		
		mav.addObject("list", list);
		mav.addObject("paggingUtil", paggingUtil);
		
		return mav;
		
	}
	
}
